/*
* Copyright 2020, Zetyun DEP All rights reserved.
*/

package ${package.ServiceImpl};

import ${package.Entity}.${entity};
import ${cfg.dto}.${entity}Dto;
import com.zetyun.dep.common.core.utils.bean.BeanUtils;
import com.zetyun.dep.common.security.utils.AutoFillUtil;
import ${package.Mapper}.${table.mapperName};
import com.zetyun.dep.common.core.utils.reflect.MapUtil;
import ${package.Service}.${table.serviceName};
import com.zetyun.dep.common.core.web.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ${table.serviceImplName}  implements ${table.serviceName} {

    @Autowired
    private ${table.mapperName} ${cfg.entiName}Mapper;

    @Overrideo
    public List<${entity}> selectList(${entity}Dto ${cfg.entiName}) {
        Map<String, Object> params= MapUtil.entityToMap(${cfg.entiName});
        return ${cfg.entiName}Mapper.selectList(params);
    }

    @Override
    public ${entity} getById(Integer ${cfg.entiName}Id) {
        return ${cfg.entiName}Mapper.getById(${cfg.entiName}Id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult insert(${entity}Dto ${cfg.entiName}Dto) {
        ${entity} ${cfg.entiName} = new ${entity}();
        checkInsert${entity}(${cfg.entiName}Dto);
        int i = insert${entity}(${cfg.entiName}Dto,${cfg.entiName});
        if (i > 0) {<#list table.fields as field><#if field.keyFlag>
            Map<String, Object> map = new HashMap<String, Object>() {
                {
                put("${field.propertyName}", ${cfg.entiName}.get${cfg.capitalName}());
                }
            };</#if></#list>
            return AjaxResult.success(map);
        }
        return AjaxResult.error();
    }

    private void checkInsert${entity}(${entity}Dto ${cfg.entiName}Dto) {
    }

    private int insert${entity}(${entity}Dto ${cfg.entiName}Dto, ${entity} ${cfg.entiName}) {
        BeanUtils.copyProperties(${cfg.entiName}Dto, ${cfg.entiName});
        AutoFillUtil.insertFill(${cfg.entiName});
        return ${cfg.entiName}Mapper.insert(${cfg.entiName});
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(${entity}Dto ${cfg.entiName}Dto) {
        ${entity} ${cfg.entiName} = new ${entity}();
        BeanUtils.copyProperties(${cfg.entiName}Dto, ${cfg.entiName});
        AutoFillUtil.updateFill(${cfg.entiName});
        return ${cfg.entiName}Mapper.update(${cfg.entiName});
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
        public void deleteById(Integer ${cfg.entiName}Id) {
        ${cfg.entiName}Mapper.deleteById(${cfg.entiName}Id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
        public void deleteByIds(Integer[] ${cfg.entiName}Ids) {
        ${cfg.entiName}Mapper.deleteByIds(${cfg.entiName}Ids);
    }
}